package org.monster.log;

import org.monster.context.core.communicator.DosHeader;
import org.monster.uiboot.UILog;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 通用日志器
 */
public class CommonLogger {

    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(DosHeader.class
            .getName());

    /**
     * 获取日志器
     *
     * @param name 名称
     * @return 日志器
     */
    public CommonLogger getLogger(String name) {
        CommonLogger logger = null;
        try {
            logger = (CommonLogger) Class.forName(CommonLogger.class.getName())
                    .newInstance();
        } catch (InstantiationException | IllegalAccessException
                | ClassNotFoundException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
        return logger;
    }

    /**
     * 日志
     *
     * @param level 级别
     * @param msg   消息
     * @param obj   对象
     */
    public void log(Level level, String msg, Object obj) {
        if (UILog.getArea() != null) {
            UILog.log(level, msg, obj);
        } else {
            logger.log(Level.SEVERE, msg, obj);
        }
    }

}
